<script setup lang="ts">
import { onLaunch, onShow, onHide, getSsrGlobalData } from "@dcloudio/uni-app";
import { store } from './store';
import { app } from './api/const';
import request from './api/request';

onLaunch(() => {
  console.log("App Launch");
  getUserLocation();
});
onShow(() => {
  console.log("App Show");
});
onHide(() => {
  console.log("App Hide");
});

const globalData:any = {}

const getUserLocation = () => {
  let that = this;
  uni.getSetting({
    success: (res:any) => {
      console.log(JSON.stringify(res))
      // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
      // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
      // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
      if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
        uni.showModal({
          title: '请求授权当前位置',
          content: '需要获取您的地理位置，请确认授权',
          success: function (res) {
            if (res.cancel) {
              uni.showToast({
                title: '拒绝授权',
                icon: 'none',
                duration: 1000
              })
            } else if (res.confirm) {
              uni.openSetting({
                success: function (dataAu) {
                  if (dataAu.authSetting["scope.userLocation"] == true) {
                    uni.showToast({
                      title: '授权成功',
                      icon: 'success',
                      duration: 1000
                    })
                    //再次授权，调用uni.getLocation的API
                    getLocation();
                  } else {
                    uni.showToast({
                      title: '授权失败',
                      icon: 'none',
                      duration: 1000
                    })
                  }
                }
              })
            }
          }
        })
      } else if (res.authSetting['scope.userLocation'] == undefined) {
        //调用uni.getLocation的API
        getLocation();
      }
      else {
        //调用uni.getLocation的API
        getLocation();
      }
    }
  })
}
// 获取定位当前位置的经纬度
const getLocation = () => {
  uni.getLocation({
    type: 'wgs84',
    success: function (res:any) {
      let latitude = res.latitude
      let longitude = res.longitude
      globalData.lat = res.latitude;//
      globalData.lng = res.longitude;//把onload定位时候的经纬度存到全局
      const requestOptions = {
        path: app.getStore,
        method: 'get',
        data: {
          latitude,
          longitude
        },
        hideLoading: true,
      };
      request(requestOptions).then((res:any) => {
        if (res.code === 200 && res.data) {
          uni.removeStorageSync('storeVo');
          const storeVo = {
            id: res.data.id,
            storeName: res.data.storeName,
            dealerId: res.data.dealerId,
            brandId: res.data.brandId,
            csPhone: res.data.csPhone,
            storeAddressDetail: res.data.storeAddressDetail,
          };
          store.commit('SET_STORE', storeVo);
          uni.setStorageSync('storeVo', storeVo);
          const pages:any = getCurrentPages()
          pages[pages.length - 1].onResize();
        }
      })
    }
  })
}

</script>
<style lang="scss">
@import 'static/iconfont.css';
</style>
